<?php

namespace App\Library\Logic;

use App\Library\Requests\Product\ProductAddRequest;
use App\Models\ShopImagesModel;

class ShopImagesLogic extends ShopImagesModel
{
    /**
     * checkProductMain
     * @param ProductAddRequest $request
     * @param int $productId
     * @return bool
     */
    public static function checkProductMain(ProductAddRequest $request, int $productId = 0): bool
    {
        if (is_null($request->mainImgIds)) return true;
        $arr = self::imgIds(4, $request->mainImgIds, $productId);
        if (empty($arr)) return true;
        $request->avatarImgIdJson = json_encode($arr);
        return false;
    }

    /**
     * checkProductDesc
     * @param ProductAddRequest $request
     * @param int $productId
     * @return bool
     */
    public static function checkProductDesc(ProductAddRequest $request, int $productId = 0): bool
    {
        if (is_null($request->descImgIds)) return false;
        $arr = self::imgIds(5, $request->descImgIds, $productId);
        if (empty($arr)) return true;
        $request->contentImgIdJson = json_encode($arr);
        return false;
    }

    /**
     * checkProductVideo
     * @param ProductAddRequest $request
     * @param int $productId
     * @return bool
     */
    public static function checkProductVideo(ProductAddRequest $request, int $productId = 0): bool
    {
        if (is_null($request->videoId)) return false;
        $where = ['imgType' => 2, 'imgSourceType' => 4, 'imgUrl' => $request->videoId];
        $self = self::findByWhere($where, null, ['imgId', 'imgSourceId']);
        if (is_null($self)) return true;
        else if ($productId > 0 && $self->img_source_id > 0 && $productId !== $self->img_source_id) return true;
        $request->avatarImgIdJson = json_encode(array_merge(json_decode($request->avatarImgIdJson, true), [
            $self->img_id
        ]));
        return false;
    }

    /**
     * imgIds
     * @param int $type
     * @param array $array
     * @param int $productId
     * @return array
     */
    public static function imgIds(int $type, array $array, int $productId): array
    {
        $arr = [];
        $list = self::query()->where('img_type', 1)->where('img_source_type', $type)
            ->whereIn('img_url', $array)->get(['img_id', 'img_source_id', 'img_url']);
        if ($list->count() === 0 || $list->count() < count($array)) return [];
        $imgUrlArr = [];
        foreach ($list as $v) {
            if ($productId > 0) {
                if ($v->img_source_id > 0 && $productId !== $v->img_source_id) continue;
                else if ($productId === $v->img_source_id) $arr[] = $v->img_id;
                else if ($v->img_source_id === 0) {
                    if (!in_array($v->img_url, $imgUrlArr)) {
                        $arr[] = $v->img_id;
                        $imgUrlArr[] = $v->img_url;
                    }
                }
            } else {
                if ($v->img_source_id === 0 && !in_array($v->img_url, $imgUrlArr)) {
                    $arr[] = $v->img_id;
                    $imgUrlArr[] = $v->img_url;
                }
            }
        }
        if (empty($arr) || count($arr) !== count($array)) return [];
        else return $arr;
    }
}
